//
//  BasicApp.m
//  FileBrowser
//
//  Created by Basuke on 08/03/18.
//  Copyright 2008 Basuke's. All rights reserved.
//

#import "BasicApp.h"

@interface BasicApp(Local)

- (void)showDebugInfo;
- (NSString *)sessionFilePath;

@end

@interface BasicApp(UIApplicationDelegate)

- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
- (void)applicationWillTerminate:(UIApplication *)application;
- (void)applicationSignificantTimeChange:(UIApplication *)application;
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation withDuration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame;
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;

@end

@implementation BasicApp

@synthesize application;
@synthesize window;
@synthesize viewController;
@synthesize session;

static BasicApp *sharedApp = nil;

+ (BasicApp *)app {
	return sharedApp;
}

- init {
	if (self = [super init]) {
		sharedApp = self;
	}
	return self;
}

- (void)restoreSession {
	self.session = [NSKeyedUnarchiver unarchiveObjectWithFile:[self sessionFilePath]];
	if (!self.session) self.session = [NSMutableDictionary dictionaryWithCapacity:10];
}

- (void)saveSession {
	NSString *path = [self sessionFilePath];
	[NSKeyedArchiver archiveRootObject:self.session toFile:path];
}

- (UIViewController *)createViewController {
	return nil;
}

- (void)windowWillDisplay {
}

- (void)windowDidDisplay {
}

@end

@implementation BasicApp(Environments)

- (NSString *)rootDirPath {
	NSString *root = [self env:@"ASPEN_SIMULATOR_ROOT"];
	if (root) return root;
	return @"/";
}

- (NSString *)homeDirPath {
	return [self env:@"HOME"];
}

- (NSString *)homeDirPath:(NSString *)name {
	return [[self homeDirPath] stringByAppendingFormat:@"/%@", name];
}

- (NSString *)documentDirPath {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *path = [paths objectAtIndex:0];
#pragma mark Begin Workaround: create application "Documents" directory if needed
	BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
	if (!exists) {
		BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:path attributes:nil];
		if (!success) {
			NSAssert(0, @"Failed to create Documents directory.");
		}
	}
#pragma mark End Workaround
	return path;
}

- (NSString *)documentDirPath:(NSString *)name {
	return [[self documentDirPath] stringByAppendingFormat:@"/%@", name];
}

- (NSString *)resourcePath:(NSString *)name {
	return [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"/%@", name];
}

- (NSString *)temporaryDirPath {
	return [self env:@"TMPDIR"];
}

- (NSString *)temporaryDirPath:(NSString *)name {
	return [[self temporaryDirPath] stringByAppendingFormat:@"/%@", name];
}

- (NSString *)env:(NSString *)key {
	return [[[NSProcessInfo processInfo] environment] objectForKey:key];
}

@end

@implementation BasicApp(UIApplicationDelegate)

- (void)applicationDidFinishLaunching:(UIApplication *)app {
	// Save window
	application = app;
	
	// Restore session
	[self restoreSession];
	
    // Create window
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
	// Set up main view controller
	self.viewController = [self createViewController];
	
	if (self.viewController) {
		[window addSubview:self.viewController.view];
	}
	
	[self windowWillDisplay];
    [window makeKeyAndVisible];
	[self windowDidDisplay];
	
//	[self showDebugInfo];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	// Save session
	[self saveSession];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
	return YES;
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
	
}

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation withDuration:(NSTimeInterval)duration {
	
}

- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation {
	
}

- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame {
	
}

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame {
	
}

@end

@implementation BasicApp(Local)

- (void)showDebugInfo {
	NSLog(@"Debug Informations");
	
	NSProcessInfo *info = [NSProcessInfo processInfo];
	NSLog(@"Name: %@", [info processName]);
	NSLog(@"Identifier: %d", [info processIdentifier]);
	NSLog(@"Processor count: %d", [info processorCount]);
	NSLog(@"Physical memory: %d", [info physicalMemory]);
	NSLog(@"Hostname: %@", [info hostName]);
	NSLog(@"Environments: %@", [info environment]);
	NSLog(@"Operating system: %@-%@ (%d)", [info operatingSystemName], [info operatingSystemVersionString], [info operatingSystem]);
	NSLog(@"rootDirPath: %@", [self rootDirPath]);
	NSLog(@"homeDirPath: %@", [self homeDirPath]);
	NSLog(@"documentDirPath: %@", [self documentDirPath]);
	NSLog(@"temporaryDirPath: %@", [self temporaryDirPath]);
	
}

- (NSString *)sessionFilePath {
	return [self documentDirPath:@"session.plist"];
}
@end
